package com.listener;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;

public class LoginSessionListener implements HttpSessionAttributeListener {

	public  Map<String,HttpSession> sessionmap = new HashMap<String,HttpSession>();
	
	/**
	 * 当session中放入数据时触发
	 */
	@Override
	public void attributeAdded(HttpSessionBindingEvent event) {
		// TODO Auto-generated method stub
		String name = event.getName();
		if(name.equals("user_id")){
			
			String userid = (String) event.getValue();
			if(sessionmap.get(userid) !=null)
			{
				 HttpSession session = sessionmap.get(userid);
				 session.removeAttribute(userid);
				 session.invalidate();
			}
			sessionmap.put(userid,event.getSession());
		}
	}
	
	/**
	 * 向session中移除数据时触发
	 */
	@Override
	public void attributeRemoved(HttpSessionBindingEvent event) {
		// TODO Auto-generated method stub
		String name = event.getName();
		if(name.equals("user_id")){
			String userid = (String) event.getValue();
			sessionmap.remove(userid);
		}
		
		
	}

	@Override
	public void attributeReplaced(HttpSessionBindingEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	public Map<String,HttpSession> getSessionmap() {
		return sessionmap;
	}

	public void setSessionmap(Map<String,HttpSession> sessionmap) {
		this.sessionmap = sessionmap;
	}

}
